package com.yoke.controller.images;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoke.common.JsonMessage;
import com.yoke.common.QiNiuFile;
import com.yoke.common.base.NginxBaseControl;
import com.yoke.controller.digging.DiggingControl;
import com.yoke.system.log.LogType;
import com.yoke.system.log.SystemLog;
import com.yoke.util.FileUtil;
import com.yoke.util.Md5Util;
import com.yoke.util.StringUtil;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;

/**
 * Created by jiangzeyin on 2017/7/11.
 */
@Controller
@RequestMapping("images")
public class FileControl extends NginxBaseControl {
    /**
     * @return
     */
    @RequestMapping(value = "file.html", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String fileIndex() {
        return "images/file";
    }

    /**
     * 接收上传文件
     *
     * @param urls
     * @param quality
     * @return
     */
    @RequestMapping(value = "file", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String file(String urls, String quality) {
        String[] urls_ = StringUtil.StringToArray(urls, "\r\n");
        if (urls_ == null || urls_.length <= 0)
            return JsonMessage.getString(400, "没有需要上传的文件");
        String doMain;
        try {
            doMain = DiggingControl.getDoMain();
        } catch (IOException e) {
            SystemLog.LOG(LogType.CONTROL_ERROR).error("上传域名解析异常", e);
            return JsonMessage.getString(401, "上传失败，系统异常");
        }
        JSONArray jsonArray = new JSONArray();
        float Quality = StringUtil.parseFloat(quality);
        if (Quality == 0.0F) {
            Quality = 0.7f;
        } else if ((Quality != 100F) && (Quality <= 0.0F || Quality >= 1.0F)) {
            Quality = 0.7f;
        }
        File path = new File(String.format("%s/temp/upload/file/%s", getBootPath(), userName));
        FileUtil.deleteDir(path);
        for (String item : urls_) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", item);
            jsonArray.add(jsonObject);
            if (StringUtil.IsEmpty(item)) {
                jsonObject.put("msg", "不能为空");
                continue;
            }
            if (!item.startsWith("http")) {
                jsonObject.put("msg", "不能为空，没有http");
                continue;
            }
            String mName = Md5Util.getMd5(item);
            String fileName = String.format("%s/%s", path.getPath(), mName);
            try {
                boolean flag = DiggingControl.httpDownload(item, fileName, true);
                if (!flag) {
                    jsonObject.put("msg", "下载失败");
                    continue;
                }
                File file = new File(fileName);
                if (!file.exists()) {
                    jsonObject.put("msg", "下载失败:-1");
                    continue;
                }
            } catch (Exception e) {
                SystemLog.LOG(LogType.CONTROL_ERROR).error("下载文件异常", e);
                jsonObject.put("msg", e.getMessage());
                continue;
            }
            String md5;
            try {
                md5 = QiNiuFile.doUploadFile(fileName, Quality);
            } catch (Exception e) {
                SystemLog.LOG(LogType.CONTROL_ERROR).error("上传文件异常", e);
                jsonObject.put("msg", e.getMessage());
                continue;
            }
            SystemLog.LOG().info(userName + " 上传：" + md5);
            jsonObject.put("url", FileUtil.ClearPath(String.format("%s/%s", doMain, md5)));
        }
        return JsonMessage.getString(200, "处理结果", jsonArray);
    }
}
